//
//  BVMyPhotoAlbumViewController.m
//  XiangYu
//
//  Created by YYKJ on 2017/6/9.
//  Copyright © 2017年 YYKJ. All rights reserved.
//  我得相册

#import "BVMyPhotoAlbumViewController.h"
#import "BVMyPhotoAlbumCell.h"

#import "BVMyPhotoAlertView.h"

#define BUY_INDEX_COLLECTIONVIEW_CELL1 @"BVIndexCell1"

@interface BVMyPhotoAlbumViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic ,strong) UICollectionView              *collectionView;
@property (nonatomic ,strong) UICollectionViewFlowLayout    *layout;
@property (nonatomic ,strong) NSMutableArray                *dataArr;
@property (nonatomic ,strong) UIImagePickerController       *imagePickerController;
@property (nonatomic ,strong) UIView                        *contentView;
@end

@implementation BVMyPhotoAlbumViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor BVBackgroundLightGrayColor]];
    [self.navigationItem setTitle:@"个人相册"];
    
    [self layoutUI];
    
}

-(void)layoutUI{
    
    _contentView = [[UIView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:_contentView];
    
    UILabel *promptLb = [[UILabel alloc]init];
    [self.view addSubview:promptLb];
    [promptLb setBackgroundColor:[UIColor whiteColor]];
    [promptLb setText:@"为了让更过的人认识你，赶快上传照片吧"];
    [promptLb setTextAlignment:NSTextAlignmentCenter];
    [promptLb setFont:[UIFont systemFontOfSize:14]];
    [promptLb setTextColor:[UIColor BVTextGrayColor]];
    promptLb.sd_layout
    .topSpaceToView(self.view, 0)
    .leftSpaceToView(self.view, 0)
    .rightSpaceToView(self.view, 0)
    .heightIs(30);
    
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
    layout.headerReferenceSize=CGSizeMake(self.view.frame.size.width,0);
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 30, ScreenW, ScreenH-64) collectionViewLayout:layout];
    [self.view addSubview :collectionView];
    [collectionView setBackgroundColor:[UIColor clearColor]];
    [collectionView setDelegate:self];
    [collectionView setDataSource:self];
    [collectionView setShowsVerticalScrollIndicator:NO];
    [collectionView setShowsHorizontalScrollIndicator:NO];
    
    [collectionView registerClass:[BVMyPhotoAlbumCell class] forCellWithReuseIdentifier:BUY_INDEX_COLLECTIONVIEW_CELL1];
    
}

#pragma mark -- UICollectionViewDataSource ,UICollectionViewDelegate
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArr.count+1;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    BVMyPhotoAlbumCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:BUY_INDEX_COLLECTIONVIEW_CELL1 forIndexPath:indexPath];
    if (indexPath.row == 0) {
        [cell.imgView setImage:[UIImage imageNamed:@"takePicture"]];
    }else{
        NSLog(@"------%@",[NSString stringWithFormat:@"%@",self.dataArr[indexPath.row-1]]);
        [cell.imgView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",self.dataArr[indexPath.row-1]]]];
    }

    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:( NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        [self launchImagePicker];
        
    }else{
        NSArray *imgUrls = @[@"http://pic.58pic.com/58pic/13/12/24/21E58PICfx8_1024.jpg",@"http://img.pconline.com.cn/images/upload/upc/tx/photoblog/1604/18/c19/20473373_1460987030407_mthumb.jpg",@"http://cdnq.duitang.com/uploads/item/201507/04/20150704223530_zWVC5.jpeg"];
        BVMyPhotoAlertView *picView = [[BVMyPhotoAlertView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH) withImgs:nil withImgUrl:imgUrls withOfset:1];
        [picView showMKPAlertView];
        
    }
    NSLog(@"-----%ld",(long)indexPath.row);
    
}

-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

#pragma mark --UICollectionViewDelegateFlowLayout
//定义每个UICollectionView 的大小
- (CGSize)collectionView:( UICollectionView *)collectionView layout:( UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:( NSIndexPath *)indexPath
{
    return CGSizeMake (ScreenW/3-5, ScreenW/3-5);
    
}

//定义每个UICollectionView 的边距
-(UIEdgeInsets )collectionView:( UICollectionView *)collectionView layout:( UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:( NSInteger )section
{
    return UIEdgeInsetsMake(2, 2, 2, 2);
}

//个是两行cell之间的间距（上下行cell的间距）
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 5;
    
}

//两个cell之间的间距（同一行的cell的间距）
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    
    return 5;
}

#pragma mark -
#pragma mark -
- (void)launchImagePicker{
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"更改头像" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *fromCamera = [UIAlertAction actionWithTitle:@"从相机选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        self.imagePickerController.allowsEditing = YES;
        [self presentViewController:self.imagePickerController animated:YES completion:nil];
    }];
    
    fromCamera.enabled = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
    
    UIAlertAction *fromGallery = [UIAlertAction actionWithTitle:@"从相册选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:self.imagePickerController animated:YES completion:nil];
    }];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    [alertController addAction:fromGallery];
    [alertController addAction:fromCamera];
    [alertController addAction:cancel];
    [self presentViewController:alertController animated:YES completion:nil];
    
}

#pragma mark -
#pragma mark - ImagePicker Delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];
    UIImage *userAvatar;
    if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
        userAvatar = [info objectForKey:UIImagePickerControllerOriginalImage];
    } else if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        userAvatar = [info objectForKey:UIImagePickerControllerEditedImage];
    }
    
    NSURL *imageURL = [info valueForKey:UIImagePickerControllerReferenceURL];
   
    
}

- (UIImagePickerController *)imagePickerController {
    if (!_imagePickerController) {
        _imagePickerController = [[UIImagePickerController alloc] init];
        _imagePickerController.delegate = self;
    }
    return _imagePickerController;
}

-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray arrayWithObjects:@"999k", nil];
    }
    return _dataArr;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
